<script setup>
import {reactive} from "vue";
import {Search} from "@element-plus/icons-vue";
import request from "@/utils/request";
import {ElMessage} from "element-plus";

/**
 * 本地存储的账户信息
 *
 * @type {any}
 */
const account = JSON.parse(localStorage.getItem('account-info') || '{}')

/**
 * 订单班列表数据
 *
 * @type {UnwrapNestedRefs<{total: number, form: {orderClassNo: string, orderClassAdministratorName: string, orderClassName: string, orderClassBusinessRoomNo: string, orderClassIntroduction: string, orderClassAdministratorNo: string, orderClassBusinessRoomName: string, studentState: string, orderClassCreateTime: string, orderClassStartTime: string, orderClassBusinessOrderClassHeadName: string, orderClassUpdateMan: string, orderClassEndTime: string, orderClassBusinessNo: string, orderClassCreateMan: string, orderClassState: number, orderClassStateName: string, orderClassBusinessName: string, orderClassMajorName: string, orderClassUpdateTime: string, orderClassRealityNumber: number, orderClassMajorNo: string}, formVisible: boolean, orderClassName: string, pageSize: number, tableData: *[], pageNum: number}>}
 */
const orderClassData = reactive({
    orderClassName: '',
    tableData: [],
    total: 0,
    pageSize: 5,
    pageNum: 1,
    formVisible: false, // 是否显示弹窗
    form: {
        orderClassNo: '',
        orderClassName: '',
        orderClassIntroduction: '',
        orderClassState: 0,
        orderClassStateName: '',
        orderClassRealityNumber: 0,
        orderClassStartTime: '',
        orderClassEndTime: '',
        orderClassAdministratorNo: '',
        orderClassAdministratorName: '',
        orderClassBusinessNo: '',
        orderClassBusinessName: '',
        orderClassBusinessOrderClassHeadName: '',
        orderClassBusinessRoomNo: '',
        orderClassBusinessRoomName: '',
        orderClassMajorNo: '',
        orderClassMajorName: '',
        orderClassCreateMan: '',
        orderClassCreateTime: '',
        orderClassUpdateMan: '',
        orderClassUpdateTime: '',
        studentState: '',
    }
})

/**
 * 学生数据
 * @type {UnwrapNestedRefs<{form: {studentOrderClassNo: string, studentIntroduction: string, studentNo: string, orderClassIntroduction: string, studentHobby: string}}>}
 */
const studentData = reactive({
    form: {
        studentNo: '',
        orderClassIntroduction: '',
        studentIntroduction: '',
        studentHobby: '',
        studentOrderClassNo: '',
    }
})

/**
 * 加载订单班
 */
const loadOrderClass = () => {
    request.get(`/api/order-class/search-order-class-student/${account.accountNo}`, {
        params: {
            pageNum: orderClassData.pageNum,
            pageSize: orderClassData.pageSize,
            orderClassName: orderClassData.orderClassName,
        }
    }).then(res => {
        orderClassData.tableData = res.data?.list || []
        orderClassData.total = res.data?.list.map(item => item.orderClassCountNumber)[0] || 0;
    })
}
// 调用订单班加载方法，获取后台数据
loadOrderClass()

/**
 * 翻页
 */
const handelCurrentChange = () => {
    // 翻页时，重新加载数据即可
    loadOrderClass()
}

/**
 * 重置
 */
const reset = () => {
    orderClassData.orderClassName = ''
    // 重置时，重新加载数据即可
    loadOrderClass()
}

/**
 * 修改学生
 *
 * @param row 按行修改
 */
const editStudent = (row) => {
    studentData.form.studentState = row.studentState
    if (studentData.form.studentState === 1 || studentData.form.studentState === 3) {
        ElMessage.info("您已报名")
    } else {
        studentData.form.studentNo = account.accountNo
        studentData.form.studentOrderClassNo = row.orderClassNo
        orderClassData.form.orderClassName = row.orderClassName
        orderClassData.form.orderClassAdministratorName = row.orderClassAdministratorName
        studentData.form.orderClassIntroduction = row.orderClassIntroduction
        studentData.form.studentHobby = row.studentHobby
        orderClassData.formVisible = true
    }
}

/**
 * 提交修改的学生
 */
const submitEditStudent = () => {
    if (account.accountRoleName === 'visitor-student') {
        ElMessage.info("游客-学生仅可以查看信息")
    } else {
        request.put(`/api/student/student-join`, studentData.form).then(res => {
            if (res.code === "200") {
                // 重新获取数据
                loadOrderClass()
                // 关闭弹窗
                orderClassData.formVisible = false
                ElMessage.success("报名成功")
            } else {
                ElMessage.error(res.msg)
            }
        })
    }
}
</script>

<template>
    <div v-if="account.accountRoleName === 'common-student' || account.accountRoleName === 'visitor-student'">
        <div class="card" style="margin-bottom: 10px;">
            <el-input style="width: 300px; margin-right: 10px;" v-model="orderClassData.orderClassName"
                      placeholder="请输入订单班名称查询"
                      :prefix-icon="Search"/>
            <el-button @click="loadOrderClass" type="primary" style="margin-left: 10px;">查询</el-button>
            <el-button @click="reset" type="info">重置</el-button>
        </div>
        <div class="card" style="margin-bottom: 10px;">
            <div>
                <el-table :data="orderClassData.tableData" style="width: 100%">
                    <el-table-column prop="orderClassNo" label="班号"/>
                    <el-table-column prop="orderClassName" label="班名"/>
                    <el-table-column prop="orderClassAdministratorName" label="订单班管理员"/>
                    <el-table-column prop="orderClassBusinessName" label="企业"/>
                    <el-table-column prop="orderClassMajorName" label="专业"/>
                    <el-table-column prop="orderClassStartTime" label="开班时间"/>
                    <el-table-column prop="orderClassEndTime" label="结班时间"/>
                    <el-table-column prop="orderClassStateName" label="状态"/>
                    <el-table-column label="操作">
                        <template #default="scope">
                            <el-button @click="editStudent(scope.row)" type="primary" size="small" plain>报名
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
        <div class="card">
            <el-pagination v-model:current-page="orderClassData.pageNum"
                           v-model:page-size="orderClassData.pageSize"
                           @current-change="handelCurrentChange"
                           background layout="prev, pager, next" :total="orderClassData.total"/>
        </div>

        <el-dialog v-model="orderClassData.formVisible" :close-on-click-modal="false" title="报名信息">
            <el-form :model="studentData.form" label-width="100px" label-position="right"
                     style="padding-right: 30px;">
                <el-form-item label="班名">
                    <el-input placeholder="无" v-model="orderClassData.form.orderClassName" autocomplete="off"
                              :maxlength="20" disabled></el-input>
                </el-form-item>
                <el-form-item label="订单班管理员">
                    <el-input placeholder="无" v-model="orderClassData.form.orderClassAdministratorName"
                              autocomplete="off"
                              :maxlength="20" disabled></el-input>
                </el-form-item>
                <el-form-item label="班级介绍">
                    <el-input
                            v-model="studentData.form.orderClassIntroduction"
                            :autosize="{ minRows: 4, maxRows: 8 }"
                            type="textarea"
                            placeholder="字数不得超过300字"
                            :maxlength="300"
                            disabled
                    />
                </el-form-item>
                <el-form-item label="自我介绍">
                    <el-input
                            v-model="studentData.form.studentIntroduction"
                            :autosize="{ minRows: 4, maxRows: 8 }"
                            type="textarea"
                            placeholder="字数不得超过300字"
                            :maxlength="300"
                    />
                </el-form-item>
                <el-form-item label="兴趣爱好">
                    <el-input
                            v-model="studentData.form.studentHobby"
                            :autosize="{ minRows: 4, maxRows: 8 }"
                            type="textarea"
                            placeholder="字数不得超过300字"
                            :maxlength="300"
                    />
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="orderClassData.formVisible = false">取消</el-button>
                    <el-button type="primary" @click="submitEditStudent">提交</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<style scoped>

</style>
